package com.aicoq.example.stock.service.impl;

import com.aicoq.example.stock.domain.StockCategoryDto;
import com.aicoq.example.stock.persist.mapper.StockCategoryMapper;
import com.aicoq.example.stock.persist.model.StockCategory;
import com.aicoq.example.stock.persist.model.example.StockCategoryExample;
import com.aicoq.example.stock.service.SyncStockCategoryService;
import com.aicoq.example.utils.http.OkHttpUtils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

@Service
public class SyncStockCategoryServiceImpl implements SyncStockCategoryService {

    @Autowired
    private StockCategoryMapper stockCategoryMapper;


    @Override
    public void syncCategory() {
        var executorService = Executors.newSingleThreadExecutor();
        executorService.execute(() -> {
            JSONArray originalCategory = queryStockFromSina();
            saveCategory(originalCategory, "", 1);
        });
    }


    private JSONArray queryStockFromSina() {
        var url = "https://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodes";
        var result = OkHttpUtils.get(url);
        return JSON.parseArray(result);
    }

    private void saveCategory(JSONArray jsonArray, String parentCategoryKey, int level) {
        var categoryName = jsonArray.getString(0);
        var childCategoryObject = jsonArray.get(1);
        var categoryKey = jsonArray.getString(2);
        if (StringUtils.isEmpty(categoryKey)) {
            categoryKey = jsonArray.getString(3);
        }

        var dataFlag = true;
        if (childCategoryObject instanceof JSONArray childCategoryArray) {
            for (var o : childCategoryArray) {
                if (o instanceof JSONArray) {
                    saveCategory((JSONArray) o, categoryKey, level + 1);
                }
            }
            dataFlag = false;
        }

        var stockCategory = new StockCategory();
        stockCategory.setParentCategoryKey(parentCategoryKey);
        stockCategory.setCategoryName(categoryName);
        stockCategory.setCategoryKey(categoryKey);
        stockCategory.setLevel(level);
        stockCategory.setDataSource("sina");
        if (dataFlag) {
            stockCategory.setDataFlag("Y");
        } else {
            stockCategory.setDataFlag("N");
        }
        StockCategoryExample queryCategoryExample = new StockCategoryExample();
        queryCategoryExample.createCriteria()
                .andCategoryKeyEqualTo(categoryKey)
                .andParentCategoryKeyEqualTo(parentCategoryKey);
        if (stockCategoryMapper.countByExample(queryCategoryExample) == 0) {
            stockCategoryMapper.insertSelective(stockCategory);
        }
    }

    private StockCategoryDto parseChildCategory(JSONArray jsonArray, String parentCategoryKey, int level) {
        String categoryName = jsonArray.getString(0);
        Object childCategoryObject = jsonArray.get(1);
        List<StockCategoryDto> childCategoryList = new ArrayList<>();
        String categoryKey = jsonArray.getString(2);
        if (StringUtils.isEmpty(categoryKey)) {
            categoryKey = jsonArray.getString(3);
        }

        if (childCategoryObject instanceof JSONArray childCategoryArray) {
            for (Object o : childCategoryArray) {
                if (o instanceof JSONArray) {
                    StockCategoryDto childCategory = parseChildCategory((JSONArray) o, categoryKey, level + 1);
                    childCategoryList.add(childCategory);
                }
            }
        }

        StockCategoryDto stockCategory = new StockCategoryDto();
        stockCategory.setParentCategoryKey(parentCategoryKey);
        stockCategory.setCategoryName(categoryName);
        stockCategory.setCategoryKey(categoryKey);
        stockCategory.setChildCategory(childCategoryList);
        stockCategory.setLevel(level);
        stockCategory.setDataSource("sina");
        if (childCategoryList.isEmpty()) {
            stockCategory.setDataFlag("Y");
        } else {
            stockCategory.setDataFlag("N");
        }
        return stockCategory;
    }
}
